package io.github.talelin.latticy.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.github.talelin.latticy.dto.SpuDTO;
import io.github.talelin.latticy.model.*;
import io.github.talelin.latticy.mapper.SpuMapper;
import io.github.talelin.latticy.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;

import javax.validation.constraints.Positive;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator@TaleLin
 * @since 2021-06-17
 */
@Service
public class SpuServiceImpl extends ServiceImpl<SpuMapper, SpuDO> implements SpuService {


    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SpuImgService spuImgService;

    @Autowired
    private SpuDetailImgService spuDetailImgService;

    @Autowired
    private SpuKeyService spuKeyService;

    public SpuDetailDO getDetail(Long id) {
        return this.baseMapper.getDetail(id);
    }

    @Transactional
    @Override
    public void create(SpuDTO spuDTO) {
        SpuDO spuDO = new SpuDO();

        CategoryDO category = categoryService.getCategoryById(spuDTO.getCategoryId());
        spuDO.setRootCategoryId(category.getParentId());

        BeanUtils.copyProperties(spuDTO, spuDO);
        this.save(spuDO);

        List<String> spuImgList = new ArrayList<>();
        if (spuDTO.getSpuImgList() == null) {
            spuImgList.add(spuDTO.getImg());
        } else {
            spuImgList = spuDTO.getSpuImgList();
        }

//        for()批量插入
        this.insertSpuImgList(spuImgList, spuDO.getId());
        if (spuDTO.getDetailImgList() != null ) {
            this.insertSpuDetailImgList(spuDTO.getDetailImgList(), spuDO.getId());
        }
        if (spuDTO.getSpecKeyIdList() != null){
            this.insertSpuKeyList(spuDTO.getSpecKeyIdList(),spuDO.getId());
        }
    }


    private void insertSpuImgList(List<String> spuImgList, Long supId) {
        List<SpuImgDO> spuImgDOList = spuImgList.stream().map((spuImg) -> {
            SpuImgDO spuImgDO = new SpuImgDO();
            spuImgDO.setImg(spuImg);
            spuImgDO.setSpuId(supId.intValue());
            return spuImgDO;
        }).collect(Collectors.toList());

        spuImgService.saveBatch(spuImgDOList);
    }

    private void insertSpuDetailImgList(List<String> spuDetailImgList, Long spuId) {
        List<SpuDetailImgDO> spuDetailDoImgList = new ArrayList<>();
        for (int i = 0; i < spuDetailImgList.size(); i++) {
            String img = spuDetailImgList.get(i);
            SpuDetailImgDO spuDetailImgDO = new SpuDetailImgDO();
            spuDetailImgDO.setImg(img)
                    .setSpuId(spuId.intValue())
                    .setIndex(i);
            spuDetailDoImgList.add(spuDetailImgDO);
        }

        spuDetailImgService.saveBatch(spuDetailDoImgList);

    }

    private void insertSpuKeyList(List<Integer> spuKeyIdList,Long spuId) {
        List<SpuKeyDO> spuKeyDOList = spuKeyIdList.stream()
                .map((sk)->{
                    SpuKeyDO spuKeyDO = new SpuKeyDO();
                    spuKeyDO.setSpecKeyId(sk)
                            .setSpuId(spuId.intValue());
                    return spuKeyDO;
                }).collect(Collectors.toList());
            this.spuKeyService.saveBatch(spuKeyDOList);
    }


}
